package com.qili.service.supervisor.impl;


import cn.hutool.core.collection.CollectionUtil;
import com.qili.entity.base.BaseDict;
import com.qili.entity.jc.dto.JcProjectDTO;
import com.qili.entity.supervisor.SupervisorProjectEvaluation;
import com.qili.entity.supervisor.dto.SupervisorProjectDTO;
import com.qili.entity.sys.SysUserProject;
import com.qili.entity.zx.dto.ZxProjectDTO;
import com.qili.mapper.supervisor.StatisticsMapper;
import com.qili.service.base.BaseDictService;
import com.qili.service.supervisor.StatisticsService;
import com.qili.service.sys.SysUserProjectService;
import com.qili.util.CommonUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import java.util.ArrayList;
import java.util.List;

@Service
public class StatisticsServiceImpl implements StatisticsService {
    @Autowired
    StatisticsMapper statisticsMapper;
    @Autowired
    private SysUserProjectService userProjectService;
    @Autowired
    private BaseDictService dictService;

    @Override
    public List<SupervisorProjectDTO> selectNumber(SupervisorProjectDTO supervisorProjectDTO) {
        //查询涉及的监理项目
        List<SupervisorProjectDTO> supervisorProjectList = statisticsMapper.selectNumber(supervisorProjectDTO);

        //查询涉及的个人工作经历中的项目:
        //暂时先不查询
//        List<SysUserProject> userProjectList = selectPersonProject(supervisorProjectDTO);

/*        if(CollectionUtil.isNotEmpty(userProjectList)){
            if(supervisorProjectList == null){
                supervisorProjectList = new ArrayList<SupervisorProjectDTO>();
            }
            mergeUserProject(supervisorProjectList,userProjectList);
        }*/

        return supervisorProjectList;
    }

    @Override
    public List<ZxProjectDTO> selectNumber(ZxProjectDTO zxProjectDTO) {
        //查询涉及的监理项目
        List<ZxProjectDTO> zxProjectDTOList = statisticsMapper.selectNumberZx(zxProjectDTO);
        return zxProjectDTOList;
    }

    @Override
    public List<JcProjectDTO> selectNumber(JcProjectDTO jcProjectDTO) {
        //查询涉及的监理项目
        List<JcProjectDTO> jcProjectDTOList = statisticsMapper.selectNumberJc(jcProjectDTO);
        return jcProjectDTOList;
    }

    /**
     * 把用户工作经历中的项目转换为监理项目对象,存入监理项目集合中待处理
     * @param supervisorProjectList
     * @param userProjectList
     */
    private void mergeUserProject(List<SupervisorProjectDTO> supervisorProjectList, List<SysUserProject> userProjectList) {
        if(CollectionUtil.isNotEmpty(userProjectList)){
            for(SysUserProject userProject : userProjectList){
                SupervisorProjectDTO aDto = new SupervisorProjectDTO();
                aDto.setHtMoney(userProject.getSupHtMoney());
                aDto.setTzMoney(userProject.getSupTzMoney());
                supervisorProjectList.add(aDto);
            }
        }
    }

    @Override
    public SupervisorProjectEvaluation selectId(String id) {
        return statisticsMapper.selectId(id);
    }

    /**
     * 根据前端传过来的角色筛选项,设置DTO,以便sql查询
     * @param supervisorProjectDTO
     */
    @Override
    public void format(SupervisorProjectDTO supervisorProjectDTO){
        if(StringUtils.isBlank(supervisorProjectDTO.getProjectRole())){
            //如果没有选择角色,把当前用户id设置为所有角色
            supervisorProjectDTO.setTechnologyPerson(supervisorProjectDTO.getUserId());
            supervisorProjectDTO.setSupervisorEngineer(supervisorProjectDTO.getUserId());
            supervisorProjectDTO.setProjectManagerPerson(supervisorProjectDTO.getUserId());
        }else{
            //如果有角色筛选:
            String projectRole = supervisorProjectDTO.getProjectRole();
            switch (projectRole){
                case "jsfzr":
                    supervisorProjectDTO.setTechnologyPerson(supervisorProjectDTO.getUserId());
                    break;
                case "xmjl":
                    supervisorProjectDTO.setProjectManagerPerson(supervisorProjectDTO.getUserId());
                    break;
                case "zjlgcs":
                    supervisorProjectDTO.setSupervisorEngineer(supervisorProjectDTO.getUserId());
                    break;
            }
        }
    }

    @Override
    public void format(ZxProjectDTO zxProjectDTO) {
        if(StringUtils.isBlank(zxProjectDTO.getProjectRole())){
            //如果没有选择角色,把当前用户id设置为所有角色
            zxProjectDTO.setTechnologyPerson(zxProjectDTO.getUserId());
            zxProjectDTO.setSupervisorEngineer(zxProjectDTO.getUserId());
            zxProjectDTO.setProjectManagerPerson(zxProjectDTO.getUserId());
        }else{
            //如果有角色筛选:
            String projectRole = zxProjectDTO.getProjectRole();
            switch (projectRole){
                case "zxdwjsfzr":
                    zxProjectDTO.setTechnologyPerson(zxProjectDTO.getUserId());
                    break;
                case "zxxmjl":
                    zxProjectDTO.setProjectManagerPerson(zxProjectDTO.getUserId());
                    break;
                case "zxdwhyfzr":
                    zxProjectDTO.setIndustryPerson(zxProjectDTO.getUserId());
                    break;
            }
        }
    }

    @Override
    public void format(JcProjectDTO jcProjectDTO) {
        if(StringUtils.isBlank(jcProjectDTO.getProjectRole())){
            //如果没有选择角色,把当前用户id设置为所有角色
            jcProjectDTO.setTechnologyPerson(jcProjectDTO.getUserId());
            jcProjectDTO.setSupervisorEngineer(jcProjectDTO.getUserId());
            jcProjectDTO.setProjectManagerPerson(jcProjectDTO.getUserId());
        }else{
            //如果有角色筛选:
            String projectRole = jcProjectDTO.getProjectRole();
            switch (projectRole){
                case "jcjsfzr":
                    jcProjectDTO.setTechnologyPerson(jcProjectDTO.getUserId());
                    break;
                case "jcxmjl":
                    jcProjectDTO.setProjectManagerPerson(jcProjectDTO.getUserId());
                    break;
                case "jcgw":
                    jcProjectDTO.setSupervisorEngineer(jcProjectDTO.getUserId());
                    break;
            }
        }
    }

    /**
     * 根据筛选条件,查询用户工作经历中的个人项目
     * @param supervisorProjectDto
     * @return
     */
    private List<SysUserProject> selectPersonProject(SupervisorProjectDTO supervisorProjectDto) {
        //查询个人项目:
        //由于个人项目没有省\市\区\项目类型\这些个条件,所以当需要根据这些条件筛选时,不查询个人项目
        if(StringUtils.isBlank(supervisorProjectDto.getProvince()) && StringUtils.isBlank(supervisorProjectDto.getCity())
                && StringUtils.isBlank(supervisorProjectDto.getCounty()) && StringUtils.isBlank(supervisorProjectDto.getProjectType())) {

            //查询个人工作经历项目:
            Example example = new Example(SysUserProject.class);
            Example.Criteria criteria = example.createCriteria();
            //设置人员id
            criteria.andEqualTo("suId", CommonUtil.getUserId());
            //如果有角色筛选项,那么设置角色
            if (StringUtils.isNotBlank(supervisorProjectDto.getProjectRole())) {
                String projectRole = supervisorProjectDto.getProjectRole();
                BaseDict selectDict = new BaseDict();
                selectDict.setBadtTypecode("LSXMJS001");
                selectDict.setBadtGroupid(projectRole);
                List<BaseDict> select = dictService.select(selectDict);
                if(CollectionUtil.isNotEmpty(select)){
                    criteria.andEqualTo("supRole", select.get(0).getBadtCode());
                }
            }
            //如果有行业类型,那么设置所属领域:
            if (StringUtils.isNotBlank(supervisorProjectDto.getHyType())) {
                criteria.andEqualTo("supArea", supervisorProjectDto.getHyType());
            }
            //如果有服务合同金额最小值筛选项,那么设置服务合同金额最小值:
            if (StringUtils.isNotBlank(supervisorProjectDto.getHtMoneyMin())) {
                criteria.andGreaterThanOrEqualTo("supHtMoney", Double.valueOf(supervisorProjectDto.getHtMoneyMin()));
            }
            //如果有服务合同金额最大值筛选项,那么设置服务合同金额最大值:
            if (StringUtils.isNotBlank(supervisorProjectDto.getHtMoneyMax())) {
                criteria.andLessThanOrEqualTo("supHtMoney", Double.valueOf(supervisorProjectDto.getHtMoneyMax()));
            }

            //如果有项目投资额最小值筛选项,那么设置项目投资额最小值:
            if (StringUtils.isNotBlank(supervisorProjectDto.getTzMoneyMin())) {
                criteria.andGreaterThanOrEqualTo("supTzMoney", Double.valueOf(supervisorProjectDto.getTzMoneyMin()));
            }
            //如果有项目投资额最大值筛选项,那么设置项目投资额最大值:
            if (StringUtils.isNotBlank(supervisorProjectDto.getTzMoneyMax())) {
                criteria.andLessThanOrEqualTo("supTzMoney", Double.valueOf(supervisorProjectDto.getTzMoneyMax()));
            }
            return userProjectService.selectByExample(example);
        }
        return null;
    }

}
